package com.lujianfei.productprice.ui.ShopDetail;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;

import com.lujianfei.productprice.ui.cart.CartActivity;
import com.lujianfei.productprice.ui.addProduct.AddProductActivity;
import com.lujianfei.productprice.ui.addProduct.ProductEntity;

import java.util.List;

import androidx.appcompat.app.AlertDialog;

public class ShopPresenter implements ShopContract.Presenter {
    private ShopContract.View view;
    private ShopContract.Model model;

    public ShopPresenter(ShopContract.View view) {
        this.view = view;
        this.model = new ShopModel();
    }

    @Override
    public void loadData() {
        List<ProductEntity> productEntities = model.getData(view.getId());
        view.setData(productEntities);
    }

    @Override
    public void openAddProduct() {
        Intent intent = new Intent(view.getContext(), AddProductActivity.class);
        intent.putExtra("shopId",view.getId());
        view.getContext().startActivity(intent);
    }

    @Override
    public void openCart() {
        Intent intent = new Intent(view.getContext(), CartActivity.class);
        intent.putExtra("shopId",view.getId());
        intent.putExtra("shopName",view.getShopName());
        view.getContext().startActivity(intent);
    }

    @Override
    public void addToCart(ProductEntity data) {
        model.addToCart(data);
        view.refreshQuantity();
    }

    @Override
    public void showLongClickDialog(final ProductEntity productEntity) {
        final CharSequence[] items = new CharSequence[]{"编辑","删除","取消"};
        new AlertDialog.Builder(view.getContext()).setTitle("提示").setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                CharSequence item = items[which];
                if ("编辑".equalsIgnoreCase(""+item)){
                    Intent intent = new Intent(view.getContext(), AddProductActivity.class);
                    intent.putExtra("id",productEntity.getId());
                    intent.putExtra("shopId",productEntity.getShopId());
                    view.getContext().startActivity(intent);
                }else if ("删除".equalsIgnoreCase(""+item)){
                    confirmDialog(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            model.delete(productEntity);
                            loadData();
                        }
                    });
                }
            }
        }).show();
    }

    private void confirmDialog(final View.OnClickListener onOkListener){
        new AlertDialog.Builder(view.getContext()).setTitle("提示").setMessage("确定要删除该产品?")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (null!=onOkListener){
                            onOkListener.onClick(null);
                        }
                        loadData();
                    }
                }).setNegativeButton("取消",null)
                .show();
    }
}
